<template>
  <div class="envCompliance">
    <div>
      <el-row class="title">
        <el-col :span="8">
          <span class="title-name">环境达标率</span>
        </el-col>
        <!-- <el-col :span="16">
          <div class="right">
            <div class="env-type">
              <span>清洁区</span>
              <div class="clear-div"></div>
            </div>
            <div class="env-type">
              <span>半清洁区</span>
              <div class="half-clear-div"></div>
            </div>
            <div class="env-type">
              <span>污染区</span>
              <div class="pollution-div"></div>
            </div>
          </div>
        </el-col>-->
      </el-row>
    </div>
    <div id="myChart" :style="{width: '100%', height: '280px'}"></div>
  </div>
</template>
<script>
import { getPlatformEvaluate } from "@/api/platformManage";
export default {
  name: "envCompliance",
  data() {
    return {
      chart: null,
      barData: []
    };
  },
  mounted() {
    let platformId = this.$route.query.platformId;

    var _this = this;
    getPlatformEvaluate(platformId).then(res => {
      var data = res.value;
      this.barData.push(data.TEMPERATURE);
      this.barData.push(data.HUMIDITY);
      this.barData.push(data.CO2);
      this.barData.push(data.PM25);
      _this.initChart();
    });
    window.addEventListener("resize", this.resizeHandler);
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeHandler);
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    resizeHandler() {
      this.chart.resize();
    },
    initChart() {
      var chartDom = document.getElementById("myChart");
      this.chart = this.$echarts.init(chartDom);
      var option;
      option = {
        color: ["#2D9DFF", "#FFAD32", "#B6C2D2"],
        xAxis: {
          type: "category",
          data: ["温度", "湿度", "CO₂", "PM2.5"]
        },
        yAxis: {
          axisLabel: {
            formatter: `\n{value}% `
          },
          min: 0,
          max: 100
        },
        series: [
          {
            type: "bar",
            name: "污染区",
            showBackground: true,
            backgroundStyle: {
              color: "rgba(180, 180, 180, 0.2)"
            },
            data: this.barData.map(function(item) {
              return (item * 100).toFixed(0);
            }),
            barWidth: 30,
            label: {
              position: "top",
              show: true,
              color: "#000",
              formatter: `\n{c}%`
            }
          }
        ]
      };
      option && this.chart.setOption(option);
      //console.log(option);
    }
  }
};
</script>
<style lang="scss" scoped>
.envCompliance {
  background-color: #fff;
  width: 100%;
  height: 329px;
  background-color: #fff;
  border-radius: 12px;
  padding: 20px;
  .title {
    align-items: center;
    .title-name {
      font-size: 20px;
      font-weight: bold;
      line-height: 27px;
      color: #0a0a0a;
    }
    .right {
      display: flex;
      justify-content: flex-end;
      align-items: center;
    }
    .env-type {
      font-size: 12px;
      font-weight: 400;
      line-height: 16px;
      color: #262626;
      display: flex;
      align-items: center;
      margin-left: 10px;
      .clear-div {
        margin-left: 3px;
        width: 19px;
        height: 6px;
        background: #2d9dff;
      }
      .half-clear-div {
        margin-left: 3px;
        width: 19px;
        height: 6px;
        background: #ffad32;
      }
      .pollution-div {
        margin-left: 3px;
        width: 19px;
        height: 6px;
        background: #b6c2d2;
      }
    }
  }
}
</style>
